package com.pxx.service.controller;


import com.pxx.item.pojo.Category;
import com.pxx.service.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;


    @GetMapping("/list")
    public ResponseEntity<List<Category>> queryCategoryList(@RequestParam("pid") Long pid){
        if(pid ==null || pid.longValue()<0){
            //响应400.
            return ResponseEntity.badRequest().build();
        }
        //查询数据库， 获取数据
      List<Category> categoriesList =   categoryService.queryCategoryList(pid);
       if (CollectionUtils.isEmpty(categoriesList)){
           return ResponseEntity.notFound().build();
       }
        return ResponseEntity.ok(categoriesList);
    }

    @GetMapping("/names")
    public ResponseEntity<List<String>> querynameByIds(@RequestParam("ids")List<Long> ids){
        List<String> strings = categoryService.queryNamesByIds(ids);
        if(CollectionUtils.isEmpty(strings)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(strings);
    }

    /**
     * 根据3级分类id，查询1~3级的分类
     * @param id
     * @return
     */
    @GetMapping("all/level")
    public ResponseEntity<List<Category>> queryAllByCid3(@RequestParam("id") Long id){
        List<Category> list = this.categoryService.queryAllByCid3(id);
        if (list == null || list.size() < 1) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(list);
    }

}
